package cn.donting.jme.swing;

import lombok.extern.slf4j.Slf4j;

import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/**
 * 一个具有 拖动效果的 JPanel
 */
@Slf4j
public abstract class JDragPanel extends JPanel {

    public JDragPanel() {
        MouseListener mouseListener = new MouseListener();
        addMouseMotionListener(mouseListener);
    }

    private class MouseListener extends MouseAdapter  {

        @Override
        public void mouseDragged(MouseEvent e) {
            JComponent c = (JComponent) e.getSource();
            TransferHandler handler = c.getTransferHandler();
            if(handler!=null){
                handler.exportAsDrag((JComponent) e.getSource(), e, TransferHandler.LINK);
            }
        }
    }

}
